package com.atguigu.yygh.hosp.controller;

import com.atguigu.common.exception.YyghException;
import com.atguigu.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @RestController 作用1、交给Spring管理 2、(转换数据)返回json数据
 */

@Api(tags = "医院设置管理")//swagger中文提示
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin//解决跨域问题(允许跨域访问)
public class HospitalSetController {

    //    注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //    1。查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
//        调用service中的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);

    }

    //    2.删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {

        return hospitalSetService.removeById(id) ? Result.ok() : Result.fail();

    }

    //    3.条件查询带分页(分页与like一起写，本质上都是查询全部)
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
//                                  将符合的数据自动封装进对象
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

//        创建配置对象，传递当前页，每页的记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(hospitalSetQueryVo.getHosname()), HospitalSet::getHosname, hospitalSetQueryVo.getHosname());
        wrapper.eq(Strings.isNotEmpty(hospitalSetQueryVo.getHoscode()), HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        return Result.ok(pageHospitalSet);

    }

    //    4.添加医院设置
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
//        设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
//        签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        return hospitalSetService.save(hospitalSet) ? Result.ok() : Result.fail();
    }

    //    5.根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //    6.修改医院设置
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {

        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();

    }

    //    7.批量删除医院设置
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //    8.医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
//        根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
//        设置状态
        hospitalSet.setStatus(status);
//        通用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //    9.发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
//        根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //todo 发送短信
        return Result.ok();
    }
}
